package com.skivingcloud.admin.sys.service.impl;

import com.skivingcloud.admin.sys.entity.SysFormDesigner;
import com.skivingcloud.admin.sys.mapper.SysFormDesignerMapper;
import com.skivingcloud.admin.sys.service.ISysFormDesignerService;
import com.skivingcloud.common.utils.PageUtils;
import com.skivingcloud.common.utils.UuidUtil;

import net.sf.json.JSONObject;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.databind.JsonNode;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 表单设计表 服务实现类
 * </p>
 *
 * @author skivingcloud
 * @since 2022-06-30
 */
@Service
public class SysFormDesignerServiceImpl extends ServiceImpl<SysFormDesignerMapper, SysFormDesigner> implements ISysFormDesignerService {

	/**
	 * 分页查询
	 * 
	 * @param params
	 * @return
	 */
	@Override
	public PageUtils queryPage(Map<String, Object> params) {
		PageUtils pageUtils = PageUtils.getPageInfo(params);
		Page<SysFormDesigner> p = PageHelper.startPage(pageUtils.getPage(), pageUtils.getLimit());
		SysFormDesigner sysFormDesigner = (SysFormDesigner) JSONObject.toBean(JSONObject.fromObject(params.get("dataForm")),
				SysFormDesigner.class);
		QueryWrapper<SysFormDesigner> queryWrapper = new QueryWrapper<SysFormDesigner>();
		if (StringUtils.isNotBlank(sysFormDesigner.getFormCode())) {
			queryWrapper.like("form_code", "%" + sysFormDesigner.getFormCode() + "%");
		}
		if (StringUtils.isNotBlank(sysFormDesigner.getFormName())) {
			queryWrapper.like("form_name", "%" + sysFormDesigner.getFormName() + "%");
		}
		this.baseMapper.selectList(queryWrapper);
		pageUtils = new PageUtils(p);
		return pageUtils;
	}

	/**
	 * 新增一条记录
	 * 
	 * @param sysFormDesigner
	 */
	@Override
	public void add(SysFormDesigner sysFormDesigner) {
		sysFormDesigner.setId(UuidUtil.get32UUID());
		sysFormDesigner.setVersion(1);
		this.baseMapper.insert(sysFormDesigner);
		
	}

	/**
	 * 修改记录
	 * 
	 * @param sysFormDesigner
	 */
	@Override
	public void update(SysFormDesigner sysFormDesigner) {
		SysFormDesigner old = this.baseMapper.selectById(sysFormDesigner.getId());
		old.setFormCode(sysFormDesigner.getFormCode());
		old.setFormDesJson(sysFormDesigner.getFormDesJson());
		old.setFormName(sysFormDesigner.getFormName());
		old.setRemark(sysFormDesigner.getRemark());
		old.setVersion(old.getVersion() + 1);
		this.baseMapper.updateById(old);
	}

	/**
	 * 删除记录
	 * 
	 * @param ids
	 */
	@Override
	public void deleteBatch(String[] ids) {
		this.baseMapper.deleteBatchIds(Arrays.asList(ids));
	}

	/**
	 * 根据编码获取表单定义json
	 * 
	 * @param formCode
	 * @return
	 */
	@Override
	public JsonNode getFormJsonByCode(String formCode) {
		QueryWrapper<SysFormDesigner> queryWrapper = new QueryWrapper<SysFormDesigner>();
		queryWrapper.eq("form_code", formCode);
		SysFormDesigner sysFormDesigner = this.baseMapper.selectOne(queryWrapper);
		return sysFormDesigner == null ? null : sysFormDesigner.getFormDesJson();
	}

	/**
	 * 根据id查询
	 * 
	 * @param id
	 * @return
	 */
	@Override
	public SysFormDesigner selectById(String id) {
		return this.baseMapper.selectById(id);
	}

	@Override
	public List<SysFormDesigner> getBusinessTypeList() {
		QueryWrapper<SysFormDesigner> queryWrapper = new QueryWrapper<>();
		return this.baseMapper.selectList(queryWrapper);
	}

}
